package com.sks.dao.privilege.impl;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.sks.bean.pojo.HouseType;
import com.sks.bean.pojo.Users;
import com.sks.dao.base.DaoSupport;
import com.sks.dao.house.HouseTypeDao;
import com.sks.dao.privilege.UsersDao;
import com.sks.utils.MD5;

/**
* @ClassName: UsersDaoImpl
* @Description: TODO(外部用户的DAO层实现)
* @author duof duof.chen@gmail.com
* @date 2011-11-1 下午09:20:46
* 
*/
@SuppressWarnings("unchecked")
@Repository("usersDao")
@Transactional
public class UsersDaoImpl extends DaoSupport<Users> implements UsersDao {

	/* (非 Javadoc)
	* <p>Title: delete</p>
	* <p>Description: 根据给定的用户Id将外部用户的状态重置</p>
	* @param sb
	* @param entityids
	* @see com.sks.dao.privilege.UsersDao#delete(java.lang.StringBuilder, java.io.Serializable[])
	*/
	public void delete(StringBuilder sb, Serializable... entityids){
		Query query = em.createQuery("delete Users o where o.userId in ("+ sb + ")");
		for(int i=0 ; i < entityids.length ; i++){
			query.setParameter(i+2, entityids[i]);
		}
		query.executeUpdate();
	}
	
	/* (非 Javadoc)
	* <p>Title: exist</p>
	* <p>Description: 根据loginName判断登录名是否存在</p>
	* @param loginName
	* @return
	* @see com.sks.dao.privilege.UsersDao#exist(java.lang.String)
	*/
	public boolean exist(String loginName){
		long count = (Long)em.createQuery("select count(o) from Users o where o.loginName=?1")
		.setParameter(1, loginName).getSingleResult();
		return count>0;
	}
	
	/* (非 Javadoc)
	* <p>Title: validate</p>
	* <p>Description: 验证该外部用户是否存在</p>
	* @param loginName
	* @param loginPwd
	* @return
	* @see com.sks.dao.privilege.UsersDao#validate(java.lang.String, java.lang.String)
	*/
	public boolean validate(String loginName, String loginPwd){
		long count = (Long)em.createQuery("select count(o) from Users o where o.loginName=?1 and o.loginPwd=?2 and o.status=?3")
		.setParameter(1, loginName).setParameter(2, loginPwd).setParameter(3, true).getSingleResult();
		return count>0;
	}
	
	/* (非 Javadoc)
	* <p>Title: visible</p>
	* <p>Description: 设置外部用户为可用或不可用</p>
	* @param visible
	* @param sb
	* @param entityIds
	* @see com.sks.dao.privilege.UsersDao#visible(boolean, java.lang.StringBuffer, java.io.Serializable[])
	*/
	public void visible(boolean visible, StringBuffer sb, Serializable ... entityIds){
		Query query = em.createQuery("update Users o set o.status=?1 where o.userId in("+ sb.toString() +")");
		query.setParameter(1, visible);
		for(int i=0; i<entityIds.length;i++){
			query.setParameter(i+2, entityIds[i]);
		}
		query.executeUpdate();
	}
	
	/* (非 Javadoc)
	* <p>Title: visible</p>
	* <p>Description: 设置外部用户为可用或不可用</p>
	* @param visible
	* @param sb
	* @param entityIds 
	* @see com.sks.dao.privilege.UsersDao#visible(boolean, java.lang.StringBuffer, java.io.Serializable[])
	*/
	public Users findByLoginName(String loginName){
		Query query = em.createQuery("from Users  o where o.loginName ='"+ loginName +"'");
		List<Users> result = (List<Users>) query.getResultList();
		return result.get(0);
	}
	
	public void updatePassword(String loginName, String newpassword){
		em.createQuery("update Users o set o.loginPwd=?1 where o.loginName=?2")
		.setParameter(1, newpassword).setParameter(2, loginName).executeUpdate();
	}
	
}


